package com.nanbei.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanbei.eduService.entity.EduChapter;
import com.nanbei.eduService.entity.EduVideo;
import com.nanbei.eduService.entity.vo.ChapterVo;
import com.nanbei.eduService.entity.vo.VideoVo;
import com.nanbei.eduService.mapper.EduChapterMapper;
import com.nanbei.eduService.mapper.EduVideoMapper;
import com.nanbei.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoMapper eduVideoMapper;

    @Override
    public List<ChapterVo> findChapterVideoById(String courseId) {
        //先根据课程ID查询章节
        QueryWrapper<EduChapter> chapterVoQueryWrapper = new QueryWrapper();
        chapterVoQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = this.baseMapper.selectList(chapterVoQueryWrapper);

        //根据章节id查询小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoMapper.selectList(eduVideoQueryWrapper);
        // 定义一个最终集合保存数据
        List<ChapterVo> chapterVoList = new ArrayList<>();

        //遍历章节信息
        for (EduChapter educhapter: eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(educhapter,chapterVo);
            chapterVoList.add(chapterVo);

            //定义一个数组保存小结信息
            List<VideoVo> videoVos = new ArrayList<>();

            //遍历小节
            for (EduVideo eduVideo: eduVideos) {
                //判断小节的章节id和章节id是否一致
                if (eduVideo.getChapterId().equals(educhapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
        }
        return chapterVoList;
    }

    /**
     * 根据ID删除章节信息，章节中如果含有小节则不能删除
     * @param chapterId
     * @return
     */
    @Override
    public Boolean delChapterById(String chapterId) {
        //先根据章节ID 判断章节中是否含有小结
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        Integer integer = eduVideoMapper.selectCount(queryWrapper);
        if (integer >0){
            // 章节中含有小节则不能删除章节
            throw new GuliException(20001,"章节中含有小结不能删除");
        }else{
            int i = this.baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    /**
     * 根据课程ID删除章节信息
     * @param courseId
     */
    @Override
    public void delChapterByCourseById(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id" ,courseId);
        this.baseMapper.delete(queryWrapper);
    }
}
